var express = require('express');
var router = express.Router();
const fs = require('fs');
const path = require('path');

// 读取用户数据
function getUsers() {
  const data = fs.readFileSync(path.join(__dirname, '../db/hy/users.json'), 'utf-8');
  return JSON.parse(data);
}

// 登录接口
router.post('/login', function(req, res) {
  const { userName, passWord } = req.body;
  const users = getUsers();
  const user = users.find(u => u.userName === userName && u.passWord === passWord);
  if (!user) {
    return res.status(401).json({ code: 401, message: '用户名或密码错误' });
  }
  if (user.status !== 'active') {
    return res.status(403).json({ code: 403, message: '用户已被禁用' });
  }
  // 返回用户基本信息
  res.json({
    code: 200,
    message: '登录成功',
    data: {
      id: user._id,
      userName: user.userName,
      realName: user.realName,
      email: user.email,
      phone: user.phone
    }
  });
});

/* GET users listing. */
router.get('/', function(req, res, next) {
  res.send('respond with a resource');
});

module.exports = router;
